/**********************************************************************************************//**
*\file comAPI.h
*\author Daneluzzi F., Pelloux-Prayer J., Battandier P.
*\version 1
*\date Novembre 2011
*\brief Biblioth�que offrant une couche au dessus de la liason s�rie afin de proposer l'envoi de trames API
*************************************************************************************************************************/

#ifndef COMAPI_H_INCLUDED
#define COMAPI_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>

#ifdef SENSOR_PC 
    #include "lienSeriePC.h"
#else 
    #include "../Util/lienSerieMC.h"
#endif

#define MAX_PAYLOAD 84  // Taille maximale des trames API

// HEADER SIZE :
#define HS_RCV_PACKET 13
#define HS_TX_PACKET  15
#define HS_TX_STATUS   8
#define HS_AT_REQUEST  5
#define HS_AT_RESPONSE 6


typedef struct _ReceivePacket
{
    unsigned char srcAdd64H[4];
    unsigned char srcAdd64L[4];
    unsigned char srcAddNet[2];
    unsigned char rcvOptions;
    unsigned char rcvData[MAX_PAYLOAD - HS_RCV_PACKET];

}ReceivePacket;

typedef struct _TransmitRequest
{
    unsigned char frameID;
    unsigned char destAdd64H[4];
    unsigned char destAdd64L[4];
    unsigned char destAddNet[2];
    unsigned char broadcastRadius;
    unsigned char options;
    unsigned char dataToSend[MAX_PAYLOAD - HS_TX_PACKET];

}TransmitRequest;

typedef struct _TransmitStatus
{
    unsigned char frameID;
    unsigned char destAddNet[2];
    unsigned char transmitRetryCount;
    unsigned char deliveryStatus;
    unsigned char discoveryStatus;
    unsigned char unusedBytes[MAX_PAYLOAD - HS_TX_STATUS];

}TransmitStatus;

typedef struct _ATRequest
{
    unsigned char frameID;
    unsigned char ATCommand[2];
    unsigned char Parameter[MAX_PAYLOAD - HS_AT_REQUEST];

}ATRequest;

typedef struct _ATResponse
{
    unsigned char frameID;
    unsigned char ATCommand[2];
    unsigned char cmdStatus;
    unsigned char cmdData[MAX_PAYLOAD - HS_AT_RESPONSE];

}ATResponse;

typedef struct _FrameAPI
{
    unsigned char length_MSB;
    unsigned char length_LSB;
    union _content
    {
        unsigned char data[MAX_PAYLOAD];

        struct _frame
        {
            unsigned char frameType;

            union _api
            {
                ReceivePacket receivePacket;

                TransmitStatus transmitStatus;

                TransmitRequest transmitRequest;

                ATRequest atRequest;

                ATResponse atResponse;

            }api;

            unsigned char checksum;

        }frame;

    }content;

}FrameAPI;

// Caract�res devant �tre �chaper pour l'envoi dans des trames API :
#define FRAME_DELIMITER             0x7E
#define ESCAPE                      0x7D
#define XON                         0x11
#define XOFF                        0x13


// Les Identifiants de commande API :
#define API_CMD_AT_CMD              0x08
#define API_CMD_AT_CMD_PARAMETER    0x09
#define API_CMD_TX_REQUEST          0x10
#define API_CMD_REMOTE_CMD          0x17
#define API_CMD_CREATE_SRC_ROUTE    0x21
#define API_CMD_AT_CMD_RESPONSE     0x88
#define API_CMD_MODEM_STATUS        0x8A
#define API_CMD_TX_STATUS           0x8B
#define API_CMD_RECV_PACKET         0x90


#define DEST_ADDRESS_SIZE           8
#define NET_ADDRESS_SIZE            2
#define FRAME_TYPE_SIZE             1
#define FRAME_ID_SIZE               1
#define BROADCAST_RADIUS_SIZE       1
#define OPTION_SIZE                 1

/*********************************************************************************************//**
* \func: makeDataTransmitFrame
*
* \brief
* Cette fonction constitue une trame API pour la transmission de donn�es.
*
*
* PARAMETRES :
* \param [in] data : Les données de la trame
* \param [in] dataLen : Les tailles des données de la trame
* \param [in] dest_H : La partie haute de l'adresse de destination.
* \param [in] dest_L : La partie basse de l'adresse.
* \param [in] dest_16bits : l'adresse sur 16 bit du module destination.
*************************************************************************************************/
void makeDataTransmitFrame(unsigned char* data, int dataLen, unsigned char dest_H[4], unsigned char dest_L[4], unsigned char dest_16bits[2]);

/*********************************************************************************************//**
* \func: sendAPI
*
* \brief
* Cette fonction transmet une trame API � travers le lien s�rie.
*
*
* PARAMETRES :
* Aucun
*************************************************************************************************/
void sendAPI(void);

/*********************************************************************************************//**
* \func: makeATFrame
*
* \brief
* Cette fonction constitue une trame API pour l'envoi de commande AT au module.
*
*
* PARAMETRES :
* \param [in] ATCmd : La commande AT � envoyer au module.
* \param [in] parameter : Le param�tre de la commande AT.
* \param [in] frameID : L'identifiant de la trame.
*************************************************************************************************/
void makeATFrame(unsigned char* ATCmd, unsigned char* parameter, unsigned char frameID);

/*********************************************************************************************//**
* \func: Checksum
*
* \brief
* Cette fonction calcule le checksum d'une trame API.
*
*
* PARAMETRES :
* Aucun
*************************************************************************************************/
unsigned char Checksum(void);

#endif // COMAPI_H_INCLUDED
